package com.ruoyi.safe.service.impl;

import com.ruoyi.common.config.MinIoProperties;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NameUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.MinIoUtil;
import com.ruoyi.safe.domain.BizSafeDocAttach;
import com.ruoyi.safe.domain.vo.BizSafeDocAttachVO;
import com.ruoyi.safe.mapper.BizSafeDocAttachMapper;
import com.ruoyi.safe.service.IBizSafeDocAttachService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 安全资料文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-15
 */
@Service
public class BizSafeDocAttachServiceImpl implements IBizSafeDocAttachService 
{
    @Autowired
    MinIoProperties minIoProperties;
    @Autowired
    BizSafeDocAttachMapper bizSafeDocAttachMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 查询安全资料文件
     * 
     * @param id 安全资料文件主键
     * @return 安全资料文件
     */
    @Override
    public BizSafeDocAttach selectBizSafeDocAttachById(Long id)
    {
        return bizSafeDocAttachMapper.selectBizSafeDocAttachById(id);
    }

    /**
     * 查询安全资料文件列表
     * 
     * @param bizSafeDocAttach 安全资料文件
     * @return 安全资料文件
     */
    @Override
    public List<BizSafeDocAttachVO> selectBizSafeDocAttachList(BizSafeDocAttach bizSafeDocAttach)
    {
        List<BizSafeDocAttach> safeDocAttaches =  bizSafeDocAttachMapper.selectBizSafeDocAttachList(bizSafeDocAttach);
        Set<String> userNames = safeDocAttaches.stream().map(item->item.getCreateBy()).collect(Collectors.toSet());
        List<BizSafeDocAttachVO> bizSafeDocAttachVOS = safeDocAttaches.stream().map(item->{
            BizSafeDocAttachVO bizSafeDocAttachVO = new BizSafeDocAttachVO();
            BeanUtils.copyProperties(item,bizSafeDocAttachVO);
            bizSafeDocAttachVO.setUrl(MinIoUtil.getFileUrl(minIoProperties.getBucketSafeDoc(),bizSafeDocAttachVO.getRealName()));
            bizSafeDocAttachVO.setFileId(String.valueOf(bizSafeDocAttachVO.getId()));
            return bizSafeDocAttachVO;
        }).collect(Collectors.toList());
        SysUser sysUser = new SysUser();
        sysUser.setUserNames(userNames);
       List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
       NameUtils.handleUserNameToNickName(bizSafeDocAttachVOS,sysUsers,"createBy");
       return bizSafeDocAttachVOS;
    }

    /**
     * 查询资料文件列表
     *
     * @param fileDocAttach 资料文件
     * @return 资料文件
     */
    @Override
    public List<BizSafeDocAttachVO> selectFileDocAttachListIds(BizSafeDocAttach fileDocAttach)
    {
        List<BizSafeDocAttach> fileDocAttaches =  bizSafeDocAttachMapper.selectFileDocAttachListIds(fileDocAttach);
        Set<String> userNames = fileDocAttaches.stream().map(item->item.getCreateBy()).collect(Collectors.toSet());
        List<BizSafeDocAttachVO> fileDocAttachVOS = fileDocAttaches.stream().map(item->{
            BizSafeDocAttachVO fileDocAttachVO = new BizSafeDocAttachVO();
            BeanUtils.copyProperties(item, fileDocAttachVO);
            fileDocAttachVO.setUrl(MinIoUtil.getFileUrl(minIoProperties.getBucketSafeDoc(),fileDocAttachVO.getRealName()));
            fileDocAttachVO.setFileId(String.valueOf(fileDocAttachVO.getId()));
            return fileDocAttachVO;
        }).collect(Collectors.toList());
        SysUser sysUser = new SysUser();
        sysUser.setUserNames(userNames);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        NameUtils.handleUserNameToNickName(fileDocAttachVOS,sysUsers,"createBy");
        return fileDocAttachVOS;
    }

    /**
     * 新增安全资料文件
     * 
     * @param bizSafeDocAttach 安全资料文件
     * @return 结果
     */
    @Override
    public int insertBizSafeDocAttach(BizSafeDocAttach bizSafeDocAttach)
    {
        bizSafeDocAttach.setCreateTime(DateUtils.getNowDate());
        bizSafeDocAttach.setCreateBy(SecurityUtils.getUsername());
        return bizSafeDocAttachMapper.insertBizSafeDocAttach(bizSafeDocAttach);
    }

    /**
     * 新增安全资料文件（未登录时）
     *
     * @param bizSafeDocAttach 安全资料文件
     * @return 结果
     */
    @Override
    public int insertBizSafeDocAttachNoLogin(BizSafeDocAttach bizSafeDocAttach)
    {
        return bizSafeDocAttachMapper.insertBizSafeDocAttach(bizSafeDocAttach);
    }
    /**
     * 修改安全资料文件
     * 
     * @param bizSafeDocAttach 安全资料文件
     * @return 结果
     */
    @Override
    public int updateBizSafeDocAttach(BizSafeDocAttach bizSafeDocAttach)
    {
        bizSafeDocAttach.setUpdateTime(DateUtils.getNowDate());
        bizSafeDocAttach.setUpdateBy(SecurityUtils.getUsername());
        return bizSafeDocAttachMapper.updateBizSafeDocAttach(bizSafeDocAttach);
    }

    /**
     * 批量删除安全资料文件
     * 
     * @param ids 需要删除的安全资料文件主键
     * @return 结果
     */
    @Override
    public int deleteBizSafeDocAttachByIds(Long[] ids)
    {
        return bizSafeDocAttachMapper.deleteBizSafeDocAttachByIds(ids);
    }

    /**
     * 删除安全资料文件信息
     * 
     * @param id 安全资料文件主键
     * @return 结果
     */
    @Override
    public int deleteBizSafeDocAttachById(Long id)
    {
        return bizSafeDocAttachMapper.deleteBizSafeDocAttachById(id);
    }
}
